package com.cjq.dao.Impl;

import com.cjq.dao.BaseAdminDao;
import com.cjq.util.DBUtils;
import com.cjq.util.JSONUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDaoMysql extends DBUtils implements BaseAdminDao {

     //通过传入的username来修改该用户的上一次登录id和登录时间
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        try {
            String sql = "update eadmin set logintime=?,loginip=? where username=?";       //这里的time如果没有传直接=now（）
            List params = new ArrayList();
            System.out.println(date);
            String s = JSONUtil.toJSON(date);
            System.out.println(s);
            params.add(new java.sql.Timestamp(date.getTime()));   //因为我们传进来的是java.util.Date;
            System.out.println(new java.sql.Timestamp(date.getTime()));
            System.out.println(date.getTime());
            String s1 = JSONUtil.toJSON(date);
            System.out.println(s1);
            params.add(ip);
            params.add(username);
            update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

    }

    //根据账户密码验证是否正确
    @Override
    public boolean login(String username, String password) {
        try {
            String sql = "select id from eadmin where username=? and password = ?";
            List params = new ArrayList();
            params.add(username);
            params.add(password);
            ResultSet rs = query(sql, params);
            return rs.next();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return false;
    }


    /**
     * 通过传入的username来修改该用户的上一次登录id和登录时间   快递员修改登录信息
     *
     * @param sysname
     * @param date
     * @param ip
     */
    @Override
    public void updateLoginTimeForCoutier(String sysname, Date date, String ip) {
        try {
            String sql = "update courier set logintime=?,loginip=? where sysname=?";       //这里的time如果没有传直接=now（）
            List params = new ArrayList();
            params.add(new java.sql.Timestamp(date.getTime()));   //因为我们传进来的是java.util.Date;
            params.add(ip);
            params.add(sysname);
            update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
    }

    @Override
    public boolean loginForCoutier(String sysname, String password) {
        try {
            String sql = "select id from courier where sysname=? and password = ?";
            List params = new ArrayList();
            params.add(sysname);
            params.add(password);
            ResultSet rs = query(sql, params);
            return rs.next();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return false;
    }



    @Override
    public String sessionPhoneCourier(String username, String password) {
        String phone= "";
        try {
            String sql = "select sysphone from courier where sysname=? and password =?";
            List params = new ArrayList();
            params.add(username);
            params.add(password);
            ResultSet rs = query(sql, params);
            if (rs.next()){
                phone = rs.getString("sysphone");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeall();
        }

        return phone;
    }
}
